package com.chaos.auth.controller;

import com.chaos.auth.api.data.RbacUserData;
import com.chaos.auth.api.dto.auth.RbacUserDto;
import com.chaos.auth.service.RbacUserService;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.starter.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@Api(tags = "")
@RequestMapping("/rbacUser")
public class RbacUserDtoController extends BaseController<RbacUserDto> {
    @Resource
    private RbacUserService rbacUserService;

    @PostMapping("/roles/page.admin")
    @ApiOperation(value = "分页", httpMethod = "POST")
    public PageResult<RbacUserDto> rolesPage(@RequestBody @Valid PageQueryDto<RbacUserDto> data) {
        PageList<RbacUserDto> page = rbacUserService.selectByPageWithRole(data);
        return pageResult(page);
    }

    @PostMapping("/validityPeriod/page.admin")
    @ApiOperation(value = "分页", httpMethod = "POST")
    public PageResult<RbacUserDto> page(@RequestBody @Valid PageQueryDto<RbacUserData> data) {
        PageList<RbacUserData> page = rbacUserService.selectByPage(data);
        List<RbacUserData> entities = page.getList();
        List<RbacUserDto> dtos = entities.stream().map(e -> {
            RbacUserDto rd = ConvertHelper.convert(e, RbacUserDto.class);
            List<LocalDateTime> list = new ArrayList<>();
            list.add(e.getValidityPeriodBegin());
            list.add(e.getValidityPeriodEnd());
            rd.setValidityPeriod(list);
            return rd;
        }).collect(Collectors.toList());
        PageList<RbacUserDto> pageDtos = new PageList<>(dtos, page.getTotal(), page.getCurrent());
        return pageResult(pageDtos);
    }
}